package group.program.Controller;

import group.program.Service.LoginService;
import group.program.pojo.trafficPojo.Result;
import group.program.pojo.trafficPojo.User;
import group.program.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User u= loginService.login(user);
        //登录成功下发令牌
        if(u!=null){
            Map<String,Object>claims=new HashMap<>();
            claims.put("user_id",u.getUser_id());
            claims.put("full_name",u.getFull_name());
            claims.put("user_name",u.getUser_name());
            String jwt= JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");
    }
}
